package org.platform.core.filecommand;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.apache.commons.lang3.StringUtils;
import org.platform.core.JobManager;
import org.platform.tool.helper.FileHelper;
import org.quartz.JobKey;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.JobDetailImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;


/**
 * 文件监听
 *
 * @author mabb
 * @date 15-1-23
 */
@Service
@Slf4j
@Deprecated
public class BaseFileCommandListener implements FileAlterationListener {
    @Autowired
    private FileHelper fileHelper;
    @Autowired
    private JobManager jobManager;

    @Override
    public void onStart(FileAlterationObserver observer) {
        File dir = observer.getDirectory();
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }

    @Override
    public void onDirectoryCreate(File directory) {

    }

    @Override
    public void onDirectoryChange(File directory) {

    }

    @Override
    public void onDirectoryDelete(File directory) {

    }

    @Override
    public void onFileCreate(File file) {
        try {
            String filePath = file.getAbsolutePath();
            if (filePath.endsWith("tmp")) {
                return;
            }
            String fileName = file.getName();

            //todo  bug
            //文件统一备份入库
            fileHelper.copyFile(filePath, fileName);

            //TODO  文件编码格式需要统一
            String fileTxt = FileUtils.readFileToString(file, "UTF-8");

            JobDetailImpl jd = new JobDetailImpl();
            jd.getJobDataMap().put("FilePath", filePath);
            jd.getJobDataMap().put("FileName", fileName);
            jd.getJobDataMap().put("txtMsg", fileTxt);
            jd.setJobClass(BaseFileCommandJob.class);
            String cmdDesc = StringUtils.join("file_Task", fileName);
            jd.setKey(JobKey.jobKey(cmdDesc));

            SimpleTrigger trigger = TriggerBuilder.newTrigger()
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(0))
                    .startNow()
                    .build();

            jobManager.register(jd, trigger);
        } catch (IOException e) {
            log.error("异常: ", e);
        }
    }

    @Override
    public void onFileChange(File file) {
        try {
            String filePath = file.getAbsolutePath();
            String fileName = file.getName();
            String fileTxt = FileUtils.readFileToString(file);

            JobDetailImpl jd = new JobDetailImpl();
            jd.getJobDataMap().put("FilePath", filePath);
            jd.getJobDataMap().put("FileName", fileName);
            jd.getJobDataMap().put("txtMsg", fileTxt);
            jd.setJobClass(BaseFileCommandJob.class);
            String cmdDesc = StringUtils.join("file_Task", fileName);
            jd.setKey(JobKey.jobKey(cmdDesc));
            SimpleTrigger trigger = TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(0)).startNow().build();
            jobManager.register(jd, trigger);
        } catch (IOException e) {
            log.error("异常: ", e);

        }
    }

    @Override
    public void onFileDelete(File file) {

    }

    @Override
    public void onStop(FileAlterationObserver observer) {

    }
}
